#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>

#define MAXLINE 1024

typedef struct timeval TimerStruct;
typedef struct timeval* Timer;

int Socket_Recv(int socket,unsigned char* Data,size_t DataSize){
	fd_set fs_read;
	int dataHasLeft=0;
	int dataHasRead=0;
	int len=0;
	dataHasLeft=DataSize;
	int tryCount=0;
	int ret=0;
	TimerStruct timerStruct;
	Timer timer=&timerStruct;
	do{
		usleep(50000);
		FD_ZERO(&fs_read);
		FD_SET(socket, &fs_read);
		timer->tv_sec=0;
		timer->tv_usec=500000;
		ret = select(socket+1, &fs_read, NULL, NULL, timer);
		if (FD_ISSET(socket, &fs_read)) {
			len=read(socket,Data,dataHasLeft);
			if(len>0){
				dataHasLeft-=len;
				dataHasRead+=len;
				tryCount=0;
			}else if(len==0){
				return 0;
			}
		}
		else {
			FD_CLR(socket, &fs_read);
			tryCount++;
		}
	}
	while(dataHasLeft>0&&tryCount<3);
	FD_CLR(socket, &fs_read);
	int i=0;
	printf("Data HasRecv[%d]=",dataHasRead);fflush(stdout);
	for(i=0;i<dataHasRead;i++){
		printf("%02X ",Data[i]);fflush(stdout);
	}
	printf("\n");fflush(stdout);
	return dataHasRead;
}

/* int Socket_SendInThread(int socket,unsigned char* Data,size_t DataSize){
	fd_set fs_read;
	int dataHasLeft=0;
	int dataHasSend=0;
	int len=0;
	int tryCount=0;
	TimerStruct timerStruct;
	Timer timer=&timerStruct;
	
	dataHasLeft=DataSize;
	do{
		timer->tv_sec=0;
		timer->tv_usec=5*1000;
		len=write(socket,Data,dataHasLeft);
		if(len>0){
			dataHasLeft-=len;
			dataHasSend+=len;
			tryCount=0;
		}
		else if(len==0){
			return 0;
		}
		else{
			tryCount++; 
		}
	}
	while(dataHasLeft>0&&tryCount<3);
	return dataHasSend;
} */


void sendThread(void** argv){
	int sock=-1;
	const unsigned char data[]={0x21,0x31,0xff,0x20,0x02,0x1b,0x39,0xc7,0x16};
	if(argv){
		sock=(int*)*argv;
		if(sock>0){
			while(1)
			{
				write(sock,data,sizeof(data));
				usleep(1000000);
			}
		}
	}
	
}

int main(int argc,char **argv)
{
	int listenfd,connfd;
	struct sockaddr_in sockaddr;
	unsigned char buff[MAXLINE];
	int n,i;
	pthread_t sendt;
	
	memset(&sockaddr,0,sizeof(sockaddr));

	sockaddr.sin_family = AF_INET;
	sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	sockaddr.sin_port = htons(11009);

	listenfd = socket(AF_INET,SOCK_STREAM,0);

	bind(listenfd,(struct sockaddr *) &sockaddr,sizeof(sockaddr));

	listen(listenfd,1024);
	printf("Please wait for the client information\n");
	for(;;)
	{
		if((connfd = accept(listenfd,(struct sockaddr*)NULL,NULL))==-1)
		{
			printf("accpet socket error: %s errno :%d\n",strerror(errno),errno);
			continue;
		}
		else{
			pthread_create(&sendt,NULL,sendThread,&connfd);
			for(;;){
				memset(buff,0x0,sizeof(buff));
				n = Socket_Recv(connfd,buff,MAXLINE);
				if(n>0){
					printf("recv msg from client:");
					for(i=0;i<n;i++){
						printf("%02X ",buff[i]);
					}
				}
				
				
			}
		}

		close(connfd);
	}
	close(listenfd);
}